//
//  ControlTemplateViewBadReplyController.m
//  Uhomebk
//
//  Created by Chiang on 2017/10/12.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ControlTemplateViewBadReplyController.h"
#import "SoonAnswerViewController.h"

@interface ControlTemplateViewBadReplyController ()<SoonAnswerViewControllerDelegate> {
    UILabel *showContentlabel;
    CustomButtonView *abtn;
}
@end

@implementation ControlTemplateViewBadReplyController
static ControlTemplateViewBadReplyController *sharedManagerBadReply = nil;

+ (ControlTemplateViewBadReplyController *)sharedManagerBadReply {
    sharedManagerBadReply = [[self alloc] init];
    return sharedManagerBadReply;
}
/**
 *  初始化变量
 */
- (void)initVar {
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 145)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    maxString = [[_params valueForKey:@"contentLengthLimit"] integerValue];
    
    //是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    } else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    
    CGFloat height = 0;
    //是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {//当writable为0时可读
        NSString* valueStr = [_params valueForKey:@"attrValue"];
        CGSize size = STRING_SIZE_FONT(CGRectGetWidth(_bgMainView.bounds) - CONTENT_WIDTH - HEADSPACE - 10, valueStr, 16);
        CGFloat lineNum = size.height / 16; //计算总行数
        if (lineNum > 5) {
            height = 80;
        } else {
            if (size.height > 50) {
                height = size.height;
            } else {
                height = 50;
            }
        }
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), height + 20);
        
        showContentlabel = [[UILabel alloc] initWithFrame:CGRectMake(HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - height) / 2, CGRectGetWidth(_bgMainView.bounds) -2*HEADSPACE, height)];
        [showContentlabel setText:valueStr];
        showContentlabel.lineBreakMode = NSLineBreakByCharWrapping;
        showContentlabel.numberOfLines = 0;
        [showContentlabel setFont:kFont_16];
        [showContentlabel setTextColor:kColor_1];
        [_bgMainView addSubview:showContentlabel];
        
    } else {//可写
        contentText = [[BaseTextView alloc] initWithFrame:CGRectMake( HEADSPACE-5, 5, CGRectGetWidth(_bgMainView.frame)-2*HEADSPACE, 145-45)];
        [contentText setTextColor:kColor_1];
        [contentText setFont:kFont_16];
        [_bgMainView addSubview:contentText];
        contentText.delegate = self;
        
        //提示文本
        CGSize size = STRING_SIZE_FONT(CGRectGetWidth(contentText.bounds) - 10, [_params valueForKey:@"markedWords"], 16);
        msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, CGRectGetWidth(contentText.bounds) - 10, size.height)];
        msgLabel.lineBreakMode = NSLineBreakByCharWrapping;
        msgLabel.numberOfLines = 0;
        if ([_params valueForKey:@"markedWords"] && ![[_params valueForKey:@"markedWords"] isEqualToString:@""]) {
            [msgLabel setText:[_params valueForKey:@"markedWords"]];
        } else {
            [msgLabel setText:@"请输入内容"];
        }
        [msgLabel setFont:kFont_16];
        [msgLabel setTextColor:kColor_C];
        [contentText addSubview:msgLabel];
        
        contentText.inputAccessoryView = [self onKeyBoardFinishedButton];
        
        //快捷回复按钮
        UIButton *soonAnswer = [UIButton buttonWithType:UIButtonTypeCustom];
        [soonAnswer setTitle:@"差评原因" forState:UIControlStateNormal];
        soonAnswer.titleLabel.font = kFont_12;
        soonAnswer.backgroundColor = kColor_T;
        soonAnswer.layer.cornerRadius = 9.5;
        soonAnswer.layer.masksToBounds = YES;
        soonAnswer.frame = CGRectMake(HEADSPACE, _bgMainView.bounds.size.height-2*HEADSPACE, 70, 19);
        [soonAnswer addTarget:self action:@selector(quickReplyAction) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:soonAnswer];
        soonAnswer.hidden = NO;
        
        NSArray *initDataArray = [SEGUtils arrayWithJsonString:_params[@"initData"]];
        if (initDataArray.count == 0) {
            soonAnswer.hidden = YES;
        }
    }
}

//点击快捷回复
- (void)quickReplyAction {
    //类型查询
    SoonAnswerViewController *soonVC = [[SoonAnswerViewController alloc] init];
    soonVC.zk_delegate = self;
    soonVC.value = self.params[@"initData"];
    soonVC.typeStr = @"badReply";
    soonVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    soonVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    //设置弹出样式
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        soonVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    } else {
        soonVC.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:soonVC animated:YES completion:^{
        //
    }];
    
}

/**
 *  给多行文本控件填充值
 *  @param valueStr 接收字符串值
 */
- (void)paddingBadReply:(NSString*)valueStr {
    [contentText setText:valueStr];
    if ([[contentText text] length] > 0) {
        _valueStr = valueStr;
        [msgLabel setHidden:YES];
        _optional = @"0";
    } else {
        [msgLabel setHidden:NO];
        _optional = @"1";
    }
}

/**
 *  当数据超过5行的时候，进行缩成2行，点击下接箭头可以显示全部内容
 */
- (void)onBtnAction:(CustomButtonView*)sender {
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    NSString* valueStr = [_params valueForKey:@"initData"];
    CGSize size = STRING_SIZE_FONT(CGRectGetWidth(_bgMainView.bounds) - CONTENT_WIDTH - HEADSPACE - 10, valueStr, 16);
    
    if (![[[sender titleLabel] text] isEqualToString:@"收起"]) {
        [sender setTitle:@"收起" forState:UIControlStateNormal];
        showContentlabel.frame = CGRectMake(showContentlabel.frame.origin.x,
                                            showContentlabel.frame.origin.y,
                                            CGRectGetWidth(showContentlabel.bounds),
                                            size.height);
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), size.height + 40);
        abtn.frame = CGRectMake(TITLE_WIDTH + HEADSPACE - 10, size.height + 15, 50, 20);
    } else {
        [sender setTitle:@"展开" forState:UIControlStateNormal];
        showContentlabel.frame = CGRectMake(showContentlabel.frame.origin.x,
                                            showContentlabel.frame.origin.y,
                                            CGRectGetWidth(showContentlabel.bounds),
                                            80);
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), 120);
        abtn.frame = CGRectMake(TITLE_WIDTH + HEADSPACE - 10, 95, 50, 20);
    }
    lineView.frame = CGRectMake(lineView.frame.origin.x,
                                CGRectGetHeight(_bgMainView.bounds) - 0.5,
                                CGRectGetWidth(lineView.bounds),
                                CGRectGetHeight(lineView.bounds));
    //改变布局
    if (_badReplyDelegate) {
        [self.badReplyDelegate onFeeRefeshUI];
    }
}

/**
 *  收键盘
 */
- (void)onKeyBoardFinishedButton:(CustomButtonView*)sender {
    [contentText resignFirstResponder];
    //    NSLog(@"键盘收起时多行文本框高度 %@",NSStringFromCGRect(_bgMainView.bounds));
}

- (void)textViewDidBeginEditing:(UITextView*)textView {
    //    NSLog(@"开始编辑时文本框高度 %@",NSStringFromCGRect(textView.bounds));
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidShowNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textView, @"object", nil]];
}
- (void)textViewDidEndEditing:(UITextView*)textView {
    
    //     NSLog(@"结束编辑时文本框高度 %@",NSStringFromCGRect(textView.bounds));
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidHideNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textView, @"object", nil]];
}

/**
 *  当多行文本内容有变化，进行回调
 *  @param textView 文本实例
 */
- (void)textViewDidChange:(BaseTextView*)textView {
    if ([[textView text] length] > 0) {
        [msgLabel setHidden:YES];
        _optional = @"0";
        _valueStr = [textView text];
    } else {
        [msgLabel setHidden:NO];
        _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
        _valueStr = @"";
    }
    if ([[textView text] length] > maxString) {
        NSString* contentStr = [[textView text] substringToIndex:maxString];
        textView.text = contentStr;
    }
    _valueStr = [textView text];
}

/**
 *  当多行文本内容快要结束时，进行回调
 *  @return 返回YES值
 */
- (BOOL)textViewShouldEndEditing:(BaseTextView*)textView {
    [contentText resignFirstResponder];
    return YES;
}

//选择完快捷语句回调
- (void)SelectSoonAnswerFinished:(id)data {
    NSString *answerStr = data;
    //如果有填写 可以进行提交
    if (answerStr.length > 0) {
        [msgLabel setHidden:YES];
        contentText.text = answerStr;
        _optional = @"0";
    } else {
        [msgLabel setHidden:NO];
        _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    }
    _valueStr = answerStr;
}
@end

